import pymongo
from bson import ObjectId
from passlib.context import CryptContext

# 连接MongoDB
client = pymongo.MongoClient("mongodb://admin:123456zy@192.168.20.103:27017")
db = client["project_management"]
collection = db["users"]

# 查询所有用户
users = collection.find()
for user in users:
    print(f"ID: {user['_id']}, Email: {user['email']}, Name: {user['name']}")

# 查询特定用户
user_id = "68edf4598b71f3f2b476f69e"
print(f"\nChecking user with ID: {user_id}")
user = collection.find_one({"_id": ObjectId(user_id)})
if user:
    print(f"Found user: {user['email']} - {user['name']}")
    print(f"Hashed password: {user.get('hashed_password', 'Not found')}")
    print(f"Roles: {user.get('roles', [])}")
    print(f"Permissions: {user.get('permissions', [])}")
    
    # 更新用户密码为123456
    pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
    new_password = "123456"
    new_hash = pwd_context.hash(new_password)
    
    # 更新数据库中的密码
    result = collection.update_one(
        {"_id": ObjectId(user_id)},
        {"$set": {"hashed_password": new_hash}}
    )
    
    if result.modified_count > 0:
        print(f"Password updated successfully for user {user['email']}")
        print(f"New password: {new_password}")
    else:
        print("Failed to update password")
else:
    print("User not found")